package br.com.lojapi.enviaemail;

import br.com.lojapi.bean.Usuario;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Aluno
 */
public class EnviaEmail {

    /**
     * Metodo para enviar e-mail para usuário cadastrado.
     *
     * @param usu
     * @throws MessagingException
     */
    public void enviaEmailSimples(Usuario usuario) throws MessagingException {

        Properties props = new Properties();
        /**
         * Parâmetros de conexão com servidor Hotmail
         */
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("jeancarlonaweb@gmail.com", "jean270681");
                    }
                });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("jeancarlonaweb@gmail.com")); //Remetente

        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(usuario.getEmail())); //Destinatário(s)

//            message.setSentDate(new Date());
        message.setSubject("Dados de usuário do Hotel Senac");
        message.setText("Olá " + usuario.getNome() + "\n\nSegue os dados de usuário do "
                + "Sistema Hotel Senac: \nLogin: " + usuario.getLogin() + "\nSenha: "
                + usuario.getSenha() + "\n\natenciosamente \nJean Carlo \nHotel Senac TI");
        /**
         * Método para enviar a mensagem criada
         */
        Transport.send(message);
    }
}